<?php


class installer
{
    public static function install()
    {
        if ($_SERVER['REQUEST_METHOD'] == "GET") {
            if (isset($_GET['step'])) {
                $step = $_GET['step'];
            } else {
                $step = "0";
            }
            switch ($step) {
                case 0:
                    self::step0();
                    break;
                case 1:
                    self::step1();
                    break;
                case 2:
                    self::step2();
                    break;
                case 3:
                    self::step3();
                    break;
                case 4:
                    self::step4();
                    break;
                case 5:
                    self::step5();
                    break;
                case 6:
                    debugMessage("删除数据库",json_encode(dbTool::deleteTable("testTable")));
                    debugMessage("创建数据表",json_encode(dbTool::createTable("testTable",["user","pass","nickname"])));
                    debugMessage("插入数据",json_encode(dbTool::insert("testTable",["user","pass","nickname"],["sch","123","sch"])));
                    debugMessage("插入数据",json_encode(dbTool::insert("testTable",["user","pass","nickname"],["sch1","123","sch"])));
                    debugMessage("读取数据",json_encode(dbTool::select("testTable",["*"],[])));
                    debugMessage("删除数据",json_encode(dbTool::deleteRow("testTable",["user"=>"sch"])));
                    debugMessage("读取数据",json_encode(dbTool::select("testTable",["*"],[])));

                    break;
            }
        } elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
            switch ($_POST['command']) {
                case "testDB":
                    $type = $_POST['type'];
                    $host = $_POST['host'];
                    $user = $_POST['user'];
                    $pass = $_POST['pass'];
                    $name = $_POST['name'];
                    $port = $_POST['port'];
                    self::testDB($type, $host, $user, $pass, $name, $port);
                    break;
                case "testMail":
                    $type = $_POST['type'];
                    $smtp = $_POST['smtp'];
                    $user = $_POST['user'];
                    $pass = $_POST['pass'];
                    $mail = $_POST['mail'];
                    self::testMail($type, $smtp, $user, $pass, $mail);
                    break;
                case "saveMail":
                    $smtp = $_POST['smtp'];
                    $user = $_POST['user'];
                    $pass = $_POST['pass'];
                    $type = $_POST['type'];
                    $IllI = "lIIlIlIlIllIllIIIllIlIlIIIlllIIllIIlIlllIIllIllIIllIIlllIIllIlIllIIlIlIlIllIllIIIllIlIllIIlllIlIlIIlIlllIIlllIlIIllIIlllIIlllIlIlIIlIlIlIllIlIllIllIllIIIIlllIlIlIIlIlllIIlllIIlIllIlIllIllIIlIIlIIlIIlllIIllllIIllIllIlIllIlIIIlIIllIIIIllIlIIlIllIlIllIIlllIlIlIIlIlIIlIIllIIlIllIlIlIIIlllIIllIIlIlllIllIIlIlIllIlIllIIlllIIl";
                    $IIlI = "lIIlIlIIlIIllIIlIllIlIlIIIlllIIllIIllIlIIllIIlIIIllIlIlIIllIIlIIlIIlIlIlIllIllIlIllIlIllIIllllIIlIIllIIIIIllIlllIllIIlllIIlllIIllIIlIlIIlIIllIIlIllIlIlIIIlllIIllIIllIlIIIlllIIlIllIIlllIllIIIlIlIIlIlIlIllIllIIIllIlllIIllIIllllIIlIlllIIlllIlIIllIlIlIIllIIllIlIIlIlIlIllIlIllIllIllIlIllIIlIllIIlIlllIllIlIIlIllIIlllIllIlIIl";
                    $lIIl = "lIIlIIlllIIlllllIllIlIllIIlllIlllIIllIIlIIllIlllIllIlIllIIllIllllIIlIlIlIllIllIlIllIlIlIIIllIllllIIllIllIIllIlIlIllIIlllIIlllIIllIIlIlIIlIIllIlIIllIlIlIIIllIllllIIllIIlIIllIlllIllIlIllIllIIllIlIIlIlIlIllIllIIIllIllIIIIlllIIIlIIlIlllIllIlIIlIllIIlllIllIlIIl";
                    if ($type == "default") {
                        $smtp = stringTool::decode($IllI, XCGZS_SECRET, true);
                        $user = stringTool::decode($IIlI, XCGZS_SECRET, true);
                        $pass = stringTool::decode($lIIl, XCGZS_SECRET, true);
                    }
                    self::saveMail($smtp,$user,$pass);
                    break;
                case "creAdmin":
                    $user = $_POST['user'];
                    $name = $_POST['name'];
                    $pass = $_POST['pass'];
                    $mail = $_POST['mail'];
                    self::creAdmin($user,$name,$pass,$mail);
                    break;
            }
        }
    }

    private static function step0()
    {
        loadHead(INSTALL_STEP0_TITLE);
        self::loadNav();
        self::loadHTML("step0");
    }

    private static function step1()
    {
        loadHead(INSTALL_STEP1_TITLE);
        self::loadNav();
        self::loadHTML("step1");
    }

    private static function step2()
    {
        loadHead(INSTALL_STEP2_TITLE);
        self::loadNav();
        self::loadHTML("step2");
    }

    private static function step3()
    {
        loadHead(INSTALL_STEP3_TITLE);
        self::loadNav();
        self::loadHTML("step3");
    }
    private static function step4(){
        loadHead(INSTALL_STEP4_TITLE);
        self::loadNav();
        self::loadHTML("step4");
    }
    private static function step5(){
        loadHead(INSTALL_STEP5_TITLE);
        self::loadNav();
        self::loadHTML("step5");
        touch(INSTALLER_PATH."installed.lck");
    }

    private static function loadHTML(string $html)
    {
        include(INSTALLER_PATH . "html/" . $html . ".html");
    }

    private static function loadNav()
    {
        self::loadHTML("nav");
    }

    private static function testDB($type, $host, $user, $pass, $name, $port)
    {
        if ($type == "mysql") {
            $conn = mysqli_connect($host, $user, $pass, $name, $port);
            if (!$conn) {
                die(json_encode(["status" => "fail", "reason" => mysqli_connect_error()]));
            }
            self::creConfFile();
            file_put_contents(CONFIG_PATH . "db_config.json", json_encode(["type"=>"mysql","host" => $host, "user" => $user, "pwd" => $pass, "name" => $name, "port" => $port]));
            self::installDB($type,$conn);
        }else if ($type == "file"){
            self::creConfFile();
            file_put_contents(CONFIG_PATH . "db_config.json",json_encode(["type"=>"file"]));
            self::installDB($type);
        }
    }

    private static function installDB($type,$conn=null)
    {
        if ($type=="mysql") {
            $sql = file_get_contents(INSTALLER_PATH . "install.sql");
            $arr = explode(';', $sql);
            $counter_s = 0;
            $counter_f = 0;
            foreach ($arr as $value) {
                if (mysqli_query($conn, $value . ";")) {
                    $counter_s++;
                } else {
                    if (mysqli_error($conn) == "Query was empty") {
                        $counter_s++;
                    } else {
                        $counter_f++;
                    }
                }
            }
            mysqli_close($conn);
            $conn = null;
            unset($conn);
            die(json_encode(["status" => "finish", "successful" => $counter_s, "fail" => $counter_f]));
        }else if ($type == "file"){
            die(json_encode("yes"));
        }
    }

    private static function creConfFile()
    {
        if (!is_dir(CONFIG_PATH)) {
            if (is_file(CONFIG_PATH)) {
                unlink(CONFIG_PATH);
            }
            mkdir(CONFIG_PATH);
        }
        touch(CONFIG_PATH . "db_config.json");
        touch(CONFIG_PATH . "mail_config.json");
        touch(CONFIG_PATH . "friend_links.json");
    }

    private static function testMail($type, $smtp, $user, $pass, $mail)
    {
        $IllI = "lIIlIlIlIllIllIIIllIlIlIIIlllIIllIIlIlllIIllIllIIllIIlllIIllIlIllIIlIlIlIllIllIIIllIlIllIIlllIlIlIIlIlllIIlllIlIIllIIlllIIlllIlIlIIlIlIlIllIlIllIllIllIIIIlllIlIlIIlIlllIIlllIIlIllIlIllIllIIlIIlIIlIIlllIIllllIIllIllIlIllIlIIIlIIllIIIIllIlIIlIllIlIllIIlllIlIlIIlIlIIlIIllIIlIllIlIlIIIlllIIllIIlIlllIllIIlIlIllIlIllIIlllIIl";
        $IIlI = "lIIlIlIIlIIllIIlIllIlIlIIIlllIIllIIllIlIIllIIlIIIllIlIlIIllIIlIIlIIlIlIlIllIllIlIllIlIllIIllllIIlIIllIIIIIllIlllIllIIlllIIlllIIllIIlIlIIlIIllIIlIllIlIlIIIlllIIllIIllIlIIIlllIIlIllIIlllIllIIIlIlIIlIlIlIllIllIIIllIlllIIllIIllllIIlIlllIIlllIlIIllIlIlIIllIIllIlIIlIlIlIllIlIllIllIllIlIllIIlIllIIlIlllIllIlIIlIllIIlllIllIlIIl";
        $lIIl = "lIIlIIlllIIlllllIllIlIllIIlllIlllIIllIIlIIllIlllIllIlIllIIllIllllIIlIlIlIllIllIlIllIlIlIIIllIllllIIllIllIIllIlIlIllIIlllIIlllIIllIIlIlIIlIIllIlIIllIlIlIIIllIllllIIllIIlIIllIlllIllIlIllIllIIllIlIIlIlIlIllIllIIIllIllIIIIlllIIIlIIlIlllIllIlIIlIllIIlllIllIlIIl";
        if ($type == "default") {
            $smtp = stringTool::decode($IllI, XCGZS_SECRET, true);
            $user = stringTool::decode($IIlI, XCGZS_SECRET, true);
            $pass = stringTool::decode($lIIl, XCGZS_SECRET, true);
        }
        self::sendMail($smtp, $user, $pass, $mail);
    }

    private static function sendMail($smtp, $user, $pass, $mail_address)
    {
        require './system/core/mail/PHPMailerAutoload.php';
        $mail = new PHPMailer(false);
        $mail->isSMTP();
        $mail->Host = $smtp;
        $mail->SMTPAuth = true;
        $mail->Username = $user;
        $mail->Password = $pass;
        $mail->setFrom($user, ROOT_TITLE);
        $mail->addAddress($mail_address);
        $mail->CharSet = 'UTF-8';
        $mail->Subject = INSTALL_STEP3_MAIL_TITLE;
        $mail->Body = '<div style="width: 85%;height: 25%;padding: 15px;background-color: #3280fc;color: #FFFFFF;box-shadow: 10px 10px 5px grey;font-size: 25px;">' . INSTALL_STEP3_MAIL_CONTENT . '</div>';
        $mail->AltBody = INSTALL_STEP3_MAIL_ALTBODY;
        if ($mail->send()) {
            die(json_encode(["status" => "successful"]));
        } else {
            die(json_encode(["status" => "fail", "reason" => $mail->ErrorInfo]));
        }
    }
    private static function saveMail($smtp,$user,$pass){
        file_put_contents(CONFIG_PATH."mail_config.json",json_encode(["mail_smtp"=>$smtp,"mail_username"=>$user,"mail_password"=>$pass]));
//        touch(INSTALLER_PATH."installed.lck");
        file_put_contents(CONFIG_PATH."friend_links.json",json_encode(["xcgzschat"=>["name"=>"星辰工作室留言板","link"=>"https://chat.xcgzs.cn","openWay"=>"self","hide"=>false]]));
        die(json_encode(["status"=>"finish"]));
    }
    private static function creAdmin($user,$name,$pass,$mail){
        $a = json_decode(file_get_contents(CONFIG_PATH."db_config.json"),true);
        $c = mysqli_connect($a['host'],$a['user'],$a['pwd'],$a['name'],$a['port']);
        if (!$c){
            die(json_encode(["status"=>"connect_error","reason"=>mysqli_connect_error()]));
        }
        if (!mysqli_query($c,"INSERT INTO user(username,nickname,password,email,is_ban,ban_reason,is_admin)VALUES('$user','$name','$pass','$mail','','','yes')"))die(json_encode(["status"=>"run_error","reason"=>mysqli_error($c)]));else die(json_encode(["status"=>"successful"]));
//        touch(INSTALLER_PATH."installed.lck");
//        die(json_encode(["status"=>"successful"]));
    }
}